layui.define(['table', 'jquery', 'element', 'form', 'pearTab', 'pearMenu', 'pearFrame'], function (exports) {
    "use strict";

    var $ = layui.jquery,
        form = layui.form,
        element = layui.element,
        pearTab = layui.pearTab,
        pearMenu = layui.pearMenu,
        pearFrame = layui.pearFrame;

    var bodyFrame;
    var sideMenu;
    var bodyTab;

    /** 同 步 请 求 获 取 数 据 */
    function getData(url) {

        $.ajaxSettings.async = false;
        var data = null;

        $.get(url, function(result) {
            data = result;
        });

        $.ajaxSettings.async = true;
        return data;
    }

    var pearAdmin = new function () {

        this.render = function (option) {

            this.menuRender(option);

            this.bodyRender(option);

            this.themeRender(option);

        }

        this.menuRender = function (option) {

            /** 侧 边 菜 单 组 件 初 始 化 */
            sideMenu = pearMenu.render({
                elem: 'sideMenu', //依赖容器
                async: true, //数据形式
                theme: option.theme,
                height: '100%',
                control: option.control ? 'control' : false, // control 
                defaultMenu: 0,
                defaultOpen: 0,
                accordion: true,
                url: option.data, //数据地址
                parseData: false, //请求后是否进行数据解析 函数
                change: option.change
            })

        }

        this.bodyRender = function (option) {

            option.data = getData(option.data);

            // console.log(option.data[0].children[0]);

            var indexDt = option.data[0].children[0]

            if (option.muiltTab) {

                bodyTab = pearTab.render({
                    elem: 'content',
                    roll: true,
                    tool: true,
                    width: '100%',
                    height: '100%',
                    index: 0,
                    tabMax: 20,
                    data: [{
                        id: indexDt.id,
                        url: indexDt.href,
                        title: indexDt.title,
                        close: false
                    }] //初始化数据
                });

                // 选 项 卡 切 换 API 文 档
                bodyTab.click(function (id) {

                    // 选 项 卡 定 位
                    bodyTab.positionTab();

                    sideMenu.selectItem(id);
                })


                $("body").on("click", ".refresh", function () {

                    bodyTab.refresh(500);
                })

                sideMenu.click(function (dom, data) {

                    bodyTab.addTabOnly({
                        id: data.menuId,
                        title: data.menuTitle,
                        url: data.menuUrl,
                        icon: data.menuIcon,
                        close: true
                    }, 300);

                    compatible();
                })

            } else {

                // 选 项 卡 初 始 API 文 档
                bodyFrame = pearFrame.render({
                    elem: 'content',
                    title: '工作空间 / 首页',
                    url: option.index,
                    width: '100%',
                    height: '100%'
                });

                $("body").on("click", ".refresh", function () {
                    bodyFrame.refresh(500);
                })

                sideMenu.click(function (dom, data) {

                    compatible();
                    bodyFrame.changePage(data.menuUrl, data.menuPath, true);

                })
            }

        }

        this.themeRender = function (option) {
            
            this.menuSkin(option.theme);
        }


        this.menuSkin = function (theme) {
            $(".pear-admin").removeClass("light-theme");
            $(".pear-admin").removeClass("dark-theme");
            $(".pear-admin").addClass(theme);
        }
    };

    // $("body").on("click", ".pearson", function() {
    //     pearTab.addTabOnlyByElem("content", {
    //         id: 111,
    //         title: "个人信息",
    //         url: "view/system/person.html",
    //         close: true
    //     })
    // })

    $("body").on("click", ".collaspe,.pear-cover", function () {
        sideMenu.collaspe();
        if ($(".pear-admin").is(".pear-mini")) {
            $(".layui-icon-spread-left").addClass("layui-icon-shrink-right")
            $(".layui-icon-spread-left").removeClass("layui-icon-spread-left")
            $(".pear-admin").removeClass("pear-mini");
        } else {
            $(".layui-icon-shrink-right").addClass("layui-icon-spread-left")
            $(".layui-icon-shrink-right").removeClass("layui-icon-shrink-right")
            $(".pear-admin").addClass("pear-mini");
        }
    })

    function compatible() {
        if ($(window).width() <= 768) {
            sideMenu.collaspe();
            if ($(".pear-admin").is(".pear-mini")) {
                $(".layui-icon-spread-left").addClass("layui-icon-shrink-right")
                $(".layui-icon-spread-left").removeClass("layui-icon-spread-left")
                $(".pear-admin").removeClass("pear-mini");
            } else {
                $(".layui-icon-shrink-right").addClass("layui-icon-spread-left")
                $(".layui-icon-shrink-right").removeClass("layui-icon-shrink-right")
                $(".pear-admin").addClass("pear-mini");
            }
        }
    }

    exports('pearAdmin', pearAdmin);
})